package com.gitee.demo.utils;

/**
 * @author Tao on 2019/12/12
 */
import java.util.Date;
import java.util.TimeZone;
import org.apache.maven.shared.utils.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Months;
import org.joda.time.Seconds;
import org.joda.time.Years;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class TimeUtils {
    private static String[] TIMESTAMP_PATTERN = new String[]{"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "yyyy-MM-dd HH:mm"};
    public static final int TYPE_SECOND = 1;
    public static final int TYPE_HOUR = 2;
    public static final int TYPE_DAY = 3;
    public static final int TYPE_MONTH = 4;
    public static final int TYPE_YEAR = 5;

    public TimeUtils() {
    }

    public static Date createDate(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond) {
        DateTime date = new DateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
        return date.toDate();
    }

    public static Date createDate(int year, int monthOfYear, int dayOfMonth) {
        return createDate(year, monthOfYear, dayOfMonth, 0, 0, 0, 0);
    }

    public static Date plusDays(Date date, int days) {
        if (date != null) {
            DateTime dateTime = new DateTime(date);
            DateTime plusDays = dateTime.plusDays(days);
            return plusDays.toDate();
        } else {
            return null;
        }
    }

    public static int daysBetween(Date date1, Date date2) {
        if (date1 != null && date2 != null) {
            DateTime dt1 = new DateTime(date1);
            DateTime dt2 = new DateTime(date2);
            return Days.daysBetween(dt1, dt2).getDays();
        } else {
            throw new IllegalArgumentException("Params error.");
        }
    }

    public static int hoursBetween(Date date1, Date date2) {
        if (date1 != null && date2 != null) {
            DateTime dt1 = new DateTime(date1);
            DateTime dt2 = new DateTime(date2);
            return Hours.hoursBetween(dt1, dt2).getHours();
        } else {
            throw new IllegalArgumentException("Params error.");
        }
    }

    public static int yearsBetween(Date date1, Date date2) {
        if (date1 != null && date2 != null) {
            DateTime dt1 = new DateTime(date1);
            DateTime dt2 = new DateTime(date2);
            return Years.yearsBetween(dt1, dt2).getYears();
        } else {
            throw new IllegalArgumentException("Params error.");
        }
    }

    public static int secondsBetween(Date date1, Date date2) {
        if (date1 != null && date2 != null) {
            DateTime dt1 = new DateTime(date1);
            DateTime dt2 = new DateTime(date2);
            return Seconds.secondsBetween(dt1, dt2).getSeconds();
        } else {
            throw new IllegalArgumentException("Params error.");
        }
    }

    public static int monthsBetween(Date date1, Date date2) {
        if (date1 != null && date2 != null) {
            DateTime dt1 = new DateTime(date1);
            DateTime dt2 = new DateTime(date2);
            return Months.monthsBetween(dt1, dt2).getMonths();
        } else {
            throw new IllegalArgumentException("Params error.");
        }
    }

    public static int between(Date date1, Date date2, int type) {
        switch(type) {
            case 1:
                return secondsBetween(date1, date2);
            case 2:
                return hoursBetween(date1, date2);
            case 3:
                return daysBetween(date1, date2);
            case 4:
                return monthsBetween(date1, date2);
            case 5:
                return yearsBetween(date1, date2);
            default:
                throw new IllegalArgumentException("Params error.");
        }
    }

    public static Date plusYears(Date date, int years) {
        if (date != null) {
            DateTime dateTime = new DateTime(date);
            DateTime plusDays = dateTime.plusYears(years);
            return plusDays.toDate();
        } else {
            return null;
        }
    }

    public static Date plusWeeks(Date date, int weeks) {
        if (date != null) {
            DateTime dateTime = new DateTime(date);
            DateTime plusDays = dateTime.plusWeeks(weeks);
            return plusDays.toDate();
        } else {
            return null;
        }
    }

    public static Date plusSeconds(Date date, int seconds) {
        if (date != null) {
            DateTime dateTime = new DateTime(date);
            DateTime plusDays = dateTime.plusSeconds(seconds);
            return plusDays.toDate();
        } else {
            return null;
        }
    }

    public static Date plusMonths(Date date, int months) {
        if (date != null) {
            DateTime dateTime = new DateTime(date);
            DateTime plusDays = dateTime.plusMonths(months);
            return plusDays.toDate();
        } else {
            return null;
        }
    }

    public static Date plusMinutes(Date date, int minutes) {
        if (date != null) {
            DateTime dateTime = new DateTime(date);
            DateTime plusDays = dateTime.plusMinutes(minutes);
            return plusDays.toDate();
        } else {
            return null;
        }
    }

    public static Date plusMillis(Date date, int millis) {
        if (date != null) {
            DateTime dateTime = new DateTime(date);
            DateTime plusDays = dateTime.plusMillis(millis);
            return plusDays.toDate();
        } else {
            return null;
        }
    }

    public static Date currentTime() {
        return DateTime.now().toDate();
    }

    public static Date plusHours(Date date, int hours) {
        if (date != null) {
            DateTime dateTime = new DateTime(date);
            DateTime plusDays = dateTime.plusHours(hours);
            return plusDays.toDate();
        } else {
            return null;
        }
    }

    public static Date parseDate(String strDate) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        } else {
            String[] var1 = TIMESTAMP_PATTERN;
            int var2 = var1.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                String pattern = var1[var3];
                Date parseDateTime = parseDate(strDate, pattern);
                if (parseDateTime != null) {
                    return parseDateTime;
                }
            }

            return null;
        }
    }

    public static Date parseDate(String strDate, String pattern) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        } else {
            DateTime parseDateTime = null;

            try {
                DateTimeFormatter forPattern = DateTimeFormat.forPattern(pattern);
                parseDateTime = forPattern.parseDateTime(strDate);
            } catch (Exception var4) {
            }

            return parseDateTime != null ? parseDateTime.toDate() : null;
        }
    }

    public static String parseDate(Date date) {
        return parseDate(date, TIMESTAMP_PATTERN[0]);
    }

    public static String parseShortDate(Date date) {
        return parseDate(date, TIMESTAMP_PATTERN[1]);
    }

    public static String parseDate(Date date, String pattern) {
        if (date == null) {
            return "";
        } else {
            DateTimeFormatter forPattern = DateTimeFormat.forPattern(pattern);
            return forPattern.print(date.getTime());
        }
    }

    public static void main(String[] args) {
        DateTimeZone gmt = DateTimeZone.forTimeZone(TimeZone.getTimeZone("GMT"));
        System.out.println(gmt);
        System.out.println(parseDate("1990-04-15 00:00:00"));
    }
}

